package com.itheima03.day11Test;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

/**
 * @author 刘全
 * @date 2021/8/2
 */
public class Test7 {
    public static void main(String[] args) throws IOException {
        /**
         * 7. 键盘录入一个任意字符串，统计字符串中相邻字符出现的次数，将结果写到D盘下的count.txt文件中（面试题）
         *     例如：aaccaabaab
         *     count.txt：2a2c2a1b2a1b
         */
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入:");
        String s = sc.next();
        char[] chars = s.toCharArray();
        FileOutputStream fos = new FileOutputStream("D:\\count.txt");
        int count;
        for (int i = 0; i < chars.length; i += count) {
            int j = i+1;
            for (; j < chars.length; j++) {
                if (chars[j] != chars[i]) {
                    break;
                }
            }
            count = j - i;
            String str = count + "" + chars[i];
            byte[] bytes = str.getBytes();
            fos.write(bytes);
        }
        fos.close();
    }
}
